package cn.itcast.aliyun.oss;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.BucketInfo;
import com.piaohailin.util.Tools;
import lombok.extern.slf4j.Slf4j;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

/**
 * 演示基本操作
 *
 * @author piaohailin@163.com
 * @date 2021-03-26
 */
@Slf4j
public class AliYunOSS {

    private static String endpoint = "http://oss-cn-beijing.aliyuncs.com";
    private static String accessKeyId = "LTAI4Fzn65JTJ9SETnWdkzcr";
    private static String accessKeySecret = "RpXoQM32jNDBOrABwzUHq3dnnfwjhu";

    // 已经在OSS创建这个空间，资源存储空间。由于阿里云限制，尽量选择唯一性的名字
    // 主要在开放授权
    private static String bucketName = "piaohailin-test-oss";
//    private static String bucketName = "handmade-spu";

    private static OSS ossClient;

    /**
     * 传入OSS文件key，并删除
     *
     * @param sourceKey
     * @return
     */
    public static String moveObject(String sourceKey) {
        check();
        ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            ossClient.deleteObject(bucketName, sourceKey);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ossClient.shutdown();
        }
        return sourceKey;
    }

    /**
     * 上传到OSS
     *
     * @param filePath
     * @return
     */
    public static String putObject(String filePath) {
        check();
        String newFileKey = "";
        ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            InputStream is = new FileInputStream(filePath);
            //上传文件全名称
            newFileKey = Tools.getStringRandom(32) + Tools.getFileType(filePath);
            ossClient.putObject(bucketName, newFileKey, is);
            log.info("Object：{} 存入OSS成功。", newFileKey);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            ossClient.shutdown();
        }

        return newFileKey;
    }

    private static void check() {
        ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        if (ossClient.doesBucketExist(bucketName)) {
            System.out.println("您已经创建Bucket：" + bucketName + "。");
        } else {
            System.out.println("您的Bucket不存在，创建Bucket：" + bucketName + "。");
            ossClient.createBucket(bucketName);
        }

        BucketInfo info = ossClient.getBucketInfo(bucketName);
        System.out.println("Bucket " + bucketName + "的信息如下：");
        System.out.println("\t数据中心：" + info.getBucket().getLocation());
        System.out.println("\t创建时间：" + info.getBucket().getCreationDate());
        System.out.println("\t用户标志：" + info.getBucket().getOwner());
    }

    public static void main(String[] args) {
        String filePath = "/Users/hailin/Pictures/图片.jpg";
//        AliYunOSS.check();
//        String filename = AliYunOSS.putObject(filePath);
        AliYunOSS.moveObject("69JDXIx09T96Tom8aC8wo3170Y338265.jpg");
//        log.info("filename {}", filename);
    }
}
